using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace gTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.BackColor = ColorTranslator.FromHtml("#d4e4ff");
            this.dgvEmails.BackgroundColor = ColorTranslator.FromHtml("#d4e4ff");
            this.dgvEmails.RowHeadersDefaultCellStyle.BackColor = ColorTranslator.FromHtml("#8EC3FF");
            this.dgvEmails.ColumnHeadersDefaultCellStyle.BackColor = ColorTranslator.FromHtml("#8EC3FF");
            this.dgvEmails.RowsDefaultCellStyle.SelectionBackColor = ColorTranslator.FromHtml("#ffffcc");
            this.dgvEmails.RowsDefaultCellStyle.SelectionForeColor = SystemColors.ControlText;
            //#ADD8E6, c3d9ff
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            object[] row1 = { true, "ziqbalbh@gmail.com", "home" };
            dgvEmails.Rows.Add(row1);
            object[] row2 = { false, "ziqbalbh@hotmail.com", "home" };
            dgvEmails.Rows.Add(row2);
            object[] row3 = { false, "ziqbalbh@yahoo.com", "home" };
            dgvEmails.Rows.Add(row3);
            object[] row4 = { false, "qasim@etrisoft.com", "work" };
            dgvEmails.Rows.Add(row4);

            //dgvEmails.

            ListViewItem item1 = new ListViewItem();
            item1.Name = "item1";
            item1.Text = "Item - 1";
            listView1.Items.Add(item1);

            ListViewItem item2 = new ListViewItem();
            item2.Name = "item2";
            item2.Text = "Item - 2";
            listView1.Items.Add(item2);

            ListViewItem item3 = new ListViewItem();
            item3.Name = "item3";
            item3.Text = "Item - 3";
            listView1.Items.Add(item3);

            ListViewItem item4 = new ListViewItem();
            item4.Name = "item4";
            item4.Text = "Item - 4";
            listView1.Items.Add(item4);

            ListViewItem item5 = new ListViewItem();
            item5.Name = "item5";
            item5.Text = "Item - 5";
            listView1.Items.Add(item5);

        }

        private void dgvEmails_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            Log("dgvEmails_RowEnter", 1);
        }

        private void dgvEmails_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            Log("dgvEmails_RowLeave", 1);
        }

        private void dgvEmails_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            Log("dgvEmails_RowValidated", 1);
            Log(string.Format("Cell {0}-{1}", e.RowIndex ,e.ColumnIndex), 3);
            
        }

        private void dgvEmails_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            Log("dgvEmails_DataError", 1);
        }

        private void dgvEmails_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            Log("dgvEmails_UserDeletingRow", 1);
            DialogResult dg = MessageBox.Show("Do you really want to remove this row?", "Removing Email ...", MessageBoxButtons.YesNo);
            if (dg == DialogResult.No)
                e.Cancel = true;
        }

        private void dgvEmails_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            Log("dgvEmails_UserDeletedRow", 1);
        }

        private void dgvEmails_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            Log("dgvEmails_UserAddedRow", 1);
        }

        private void dgvEmails_SelectionChanged(object sender, EventArgs e)
        {
            Log("dgvEmails_SelectionChanged", 1);
        }

        private void Log(string message, int type)
        {
            switch (type)
            {
                case 1:
                    message = string.Format("MESSAGE: {0}", message);
                    break;
                case 2:
                    message = string.Format("ERROR: {0}", message);
                    break;
                case 3:
                    message = string.Format("WARNING: {0}", message);
                    break;
                default:
                    message = string.Format("UNKNOWN: {0}", message);
                    break;
            }

            Console.WriteLine(message);
        }

        private void dgvEmails_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Log("dgvEmails_RowsRemoved", 1);
        }

        private void dgvEmails_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Log("dgvEmails_RowsAdded", 1);
        }

        private void dgvEmails_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            Log("dgvEmails_CellValueChanged", 1);
        }

        private void dgvEmails_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            Log("dgvEmails_RowValidating", 1);
            if (dgvEmails.Rows[e.RowIndex].IsNewRow)
            {

            }
            else
            {
                if (dgvEmails.Rows[e.RowIndex].Cells[dgvEmailAddress.Name].Value == null)
                    e.Cancel = false;
            }
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            Log("listView1_ItemActivate", 1);
            
        }

        private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
        {
            Log("listView1_ItemMouseHover", 1);
            ChangeColor(e.Item);
        }

        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            Log("listView1_ItemChecked", 1);
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            Log("listView1_ItemSelectionChanged", 1);
            Log(e.IsSelected.ToString(), 1);
            Log(e.Item.Name, 1);
            ChangeChecked(e.Item);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Log("listView1_SelectedIndexChanged", 1);
        }

        private void ChangeColor(ListViewItem current)
        {
            foreach (ListViewItem item in current.ListView.Items)
            {
                item.BackColor = ColorTranslator.FromHtml("#d4e4ff");
            }
            current.BackColor = ColorTranslator.FromHtml("#ffffcc");
        }

        private void ChangeChecked(ListViewItem current)
        {
            foreach (ListViewItem item in current.ListView.Items)
            {
                item.Checked = false;
            }
            current.Checked = true;
        }

    }
}